import { ExtIdTuple, ExtUUidTuple, VARCHAR255_PG } from "~/db.pg";
import { ExtIntro, ExtOrder, Tradable } from "~/extend";

import { ExtOwner } from "@/stream-user/core.define";
import { Book } from "@/pond-book/schema.book";
import { BookMap } from "@/pond-book/schema.book";

import { ENUM_VOUCHER } from "./core.define";
import { Media } from "../pond-media/schema.media";

/** 财务记录的分组（转账） */
export type VoucherGroup = ExtIdTuple &
  ExtOwner &
  ExtIntro & {
    joinVoucherList?: Voucher[];
  };

/** 它叫做《财务记录》即会计凭证，它包含了多条（明细）账簿记录
 * @bankName 将收款/付款银行名称制作成标签
 * @contractSide 将我方公司制作成标签
 * @contractSideOp 将对方公司推送到联系人之中
 */
export type Voucher = ExtUUidTuple &
  ExtOwner &
  ExtOrder &
  Tradable & {
    /** 标签字符串 */
    labelStr: VARCHAR255_PG;
    type: ENUM_VOUCHER;
  } & {
    refVoucherGroupId: VoucherGroup["id"];
    justVoucherGroup?: VoucherGroup;

    joinRefVoucherMediaList?: RefVoucherMedia[]; // 图片
  } & {
    joinBookList?: Book[]; // 账簿记录
    joinBookMapList?: BookMap[]; // 账簿记录合计
  };

export type VoucherJoinLsKey =
  | "justOwner"
  | "justCreator"
  | "justContact"
  | "justVoucherGroup"
  | "joinRefVoucherMediaList"
  | "joinBookList"
  | "joinBookMapList";

export const VoucherJoinLs: VoucherJoinLsKey[] = [
  "justOwner",
  "justCreator",
  "justContact",
  "justVoucherGroup",
  "joinRefVoucherMediaList",
  "joinBookList",
  "joinBookMapList",
];

// ================================================
// ================================================
// ================================================
// ================================================
// ================================================
// ================================================

/** 财务记录/图片 */
export type RefVoucherMedia = ExtOwner &
  ExtIdTuple & {
    refVoucherId: Voucher["uuid32"];
    refMediaId: Media["id"];
  } & {
    justVoucher?: Voucher;
    justMedia?: Media;
  };
